<?php


namespace App\Component;


use Swoole\Table;

class SwooleTable
{

    use Singleton;

    private array $list = [];

    public function add(string $tableName, array $columns, $size)
    {
        if (!isset($this->list[$tableName])) {
            $table = new Table($size);
            foreach ($columns as $column => $value) {
                $table->column($column, $value['type'], $value['size']);
            }
            $table->create();
            $this->list[$tableName] = $table;
        }
    }

    public function get($tableName)
    {
        if (isset($this->list[$tableName])) {
            return $this->list[$tableName];
        }
        return null;
    }


    public function del($tableName)
    {
        if (isset($this->list[$tableName])) {
            $this->list[$tableName]->destroy();
            unset($this->list[$tableName]);
        }
    }
}